package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 操作附件的
 */
@Controller
@RequestMapping("/cargo/extCproduct/")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private ExtCproductService extCproductService;
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list", name = "展示购销合同货物下附件列表数据")
    public String list(String contractId, String contractProductId, @RequestParam(name = "page", defaultValue = "1") Integer pageNum, //这个name="page" 不能修改的
                       @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
//
//        需要向request域中放4个数据：1、用来生成附件的工厂  2、这个货物下的所有附件数据 3、货物id 4、合同id

//         查询用来生成货物的工厂数据 select * from co_factory where ctype='附件'
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
//        1、用来生成附件的工厂
        request.setAttribute("factoryList", factoryList);

//        查询此货物下的所有附件数据  select * from 附件表 where 货物id=？
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(example, pageNum, pageSize);
//        2、这个货物下的所有附件数据
        request.setAttribute("page", pageInfo);

//       3、 货物id
        request.setAttribute("contractProductId", contractProductId);
//       4、 为了保存货物时关键合同
        request.setAttribute("contractId", contractId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit",name = "保存购销合同货物下附件数据")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto){
//        完成七牛云上传，返回url 保存到货物图片属性上
        String upload = null;
        try {
            if(productPhoto!=null){
                upload = fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
            upload = null;
        }
        extCproduct.setProductImage(upload);

        if(StringUtils.isEmpty(extCproduct.getId())){  //ID为空表示新增
//            1、id  2、企业id和企业名称 3、创建时间
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else{
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }


    @RequestMapping(value = "/toUpdate",name = "跳转到购销合同货物下附件编辑页面")
    public String toUpdate(String id){
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //       3、 货物id
        request.setAttribute("contractProductId",extCproduct.getContractProductId());
        //       4、 为了保存货物时关键合同
        request.setAttribute("contractId",extCproduct.getContractId());

        return "cargo/extc/extc-update"; //新增和修改公用一个页面
    }

    @RequestMapping(value = "/delete",name = "删除购销合同货物下附件")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id); //删除货物
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }

}